//
//  SynthesizeView.m
//  Mahjong
//
//  Created by Chen on 16/9/7.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "SynthesizeView.h"
#import "SynthesizeViewTableViewCell.h"

static NSString *identifierForTableViewCell = @"UITableViewCell";
static NSString *identifierForSynthesizeViewTableViewCell = @"SynthesizeViewTableViewCell";

@interface SynthesizeView ()<UITableViewDelegate,UITableViewDataSource,SynthesizeViewTableViewCellDelegate>

/** tableView */
@property (nonatomic , strong) UITableView *tableView;

/** dataArray */
@property (nonatomic , strong) NSArray *dataArray;

/** currentSelectView */
@property (nonatomic , strong) UIImageView *currentSelectView;

@property (nonatomic , assign) BOOL isFirst;

/** 返回的dic */
@property (nonatomic , strong) NSMutableDictionary *selectDic;

@property (nonatomic , copy) NSString *selectMode;

@end

@implementation SynthesizeView

+ (instancetype)initSynthesizeView{
    SynthesizeView *view = [[self alloc] initWithFrame:CGRectMake(0, 40, kUIScreenWidth, kUIScreenHeight - kNavigationBarHeight - 40)];
    return view;
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.isFirst = YES;
        self.selectDic = [NSMutableDictionary dictionary];
        [self createBackView];
        [self createTableView];
        [self createDataArray];
        [self createCurrentSelectView];
    }
    return self;
}

- (void)createCurrentSelectView{
    self.currentSelectView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"service_but_finish"]];
    self.currentSelectView.frame = CGRectMake(kUIScreenWidth - 55, (65-30)/2.0, 30, 30);
}

- (void)createDataArray{
    self.dataArray = @[@"综合排序",@"新品优先",@"评价最高",@"价格区间"];
}

- (void)createBackView{
    UIView *backView = [[UIView alloc] initWithFrame:self.bounds];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.4;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(remove:)];
    [backView addGestureRecognizer:tap];
    
    [self addSubview:backView];
}


- (void)remove:(UITapGestureRecognizer *)tap{
    [self removeFromSuperview];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 300) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifierForTableViewCell];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForSynthesizeViewTableViewCell bundle:nil] forCellReuseIdentifier:identifierForSynthesizeViewTableViewCell];
    
    [self addSubview:self.tableView];
}

#pragma mark -- TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        SynthesizeViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForSynthesizeViewTableViewCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForTableViewCell forIndexPath:indexPath];
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.isFirst && indexPath.row == 0) {
        [cell addSubview:self.currentSelectView];
        self.selectMode = self.dataArray[0];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        return 105;
    }
    return 65;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 3) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell addSubview:self.currentSelectView];
        self.selectMode = self.dataArray[indexPath.row];
    }
}

- (void)clickSearchBtnWithMinPrice:(NSInteger)minPrice MaxPrice:(NSInteger)maxPrice{
    self.selectDic[@"maxPrice"] = @(maxPrice);
    self.selectDic[@"minPrice"] = @(minPrice);
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchWithMinPrice:MaxPrice:ModeName:)]) {
        [self.delegate searchWithMinPrice:minPrice MaxPrice:maxPrice ModeName:self.selectMode];
        [self removeFromSuperview];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
